package com.ld.control.business.variable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.variable.mapper.VariableMapper;
import com.ld.control.business.variable.model.VariableModel;
import com.ld.control.business.variable.service.VariableService;
import com.ld.control.business.variable.vo.req.VariableReqVo;
import com.ld.control.business.variable.vo.resp.VariableRespVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.exception.BusinessException;
import com.ld.control.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * <p>
 * 变量表 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-04-23
 */
@Service
public class VariableServiceImpl extends ServiceImpl<VariableMapper, VariableModel> implements VariableService {

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private VariableMapper variableMapper;

    @Override
    public void add(VariableReqVo reqVo) {
        checkAddOrUpdate(reqVo);
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        VariableModel variableModel = new VariableModel();
        BeanUtils.copyProperties(reqVo, variableModel);
        variableModel.setCreateBy(currentUserAccountId);
        variableModel.setCreateTime(new Date());
        variableMapper.insert(variableModel);
    }

    @Override
    public void update(VariableReqVo reqVo) {
        checkAddOrUpdate(reqVo);
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        VariableModel variableModel = new VariableModel();
        BeanUtils.copyProperties(reqVo, variableModel);
        variableModel.setUpdateBy(currentUserAccountId);
        variableModel.setUpdateTime(new Date());
        variableMapper.updateById(variableModel);
    }

    @Override
    public void deleteById(Long id) {
        variableMapper.deleteById(id);
    }

    @Override
    public VariableRespVo getById(Long id) {
        VariableRespVo variableRespVo = new VariableRespVo();
        VariableModel variableModel = variableMapper.selectById(id);
        BeanUtils.copyProperties(variableModel, variableRespVo);
        return variableRespVo;
    }

    @Override
    public GeneratePageListVo<VariableRespVo> getPageList(String name, Integer current, Integer size) {
        IPage<VariableRespVo> iPage = new Page<>(current, size);
        IPage<VariableRespVo> variableRespVoIPage = variableMapper.selectPageList(iPage, name);
        return new GeneratePageListVo<>(variableRespVoIPage);
    }

    @Override
    public VariableModel getByName(String variableName) {
        if (variableName.startsWith("@")) {
            // 去掉@前缀方便查询
            variableName = variableName.substring(1);
        }
        QueryWrapper<VariableModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(VariableModel.NAME, variableName);
        VariableModel variableModel = variableMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(variableModel)) {
            throw new BusinessException(variableName + "-变量为空！");
        }
        return variableModel;
    }

    public void checkAddOrUpdate(VariableReqVo reqVo) {
        QueryWrapper<VariableModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(VariableModel.NAME, reqVo.getName());
        if (reqVo.getId() != null) {
            queryWrapper.ne(VariableModel.ID, reqVo.getId());
        }
        Long count = variableMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException("变量名称已经存在");
        }
    }
}
